<!--
 * @FileDescription: axios重写
 * @Author: 周沿江
 * @Date: 2021/6/25
 * @LastEditors: 周沿江
 -->
// 引入 axios
import axios from 'axios'
import QS from 'qs'
axios.defaults.baseURL = '/apis';//线上地址

//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

//===================================静态数据==================================
// axios.defaults.baseURL = '';// 测试地址（主机运行数据段的
axios.interceptors.request.use(function (config) {
    // config['url'] =config['url'] +'.json';
    // config['method']='get';
    console.log(config);
    return config;
}, function (error) {
  return Promise.reject(error);
});
//=============================================================================




var http = {

  /** get 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  get: function(url,params){
    return new Promise((resolve,reject) => {
      axios.get(url,{
        params:params
      })
      .then((response) => {
        if(response.data.code ===200){
          resolve( response.data.data );
        }else {
       reject( response.data.msg );}
      })
      .catch((error) => {
        reject( error );
      });
    })
  },
  /** post 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  post: function(url,params){
    return new Promise((resolve,reject) => {
      axios.defaults.headers.post['Content-Type'] = 'application/json';
      axios.post(url,JSON.stringify(params))
      .then((response) => {
        if(response.data.code ===200){
          resolve( response.data.data );
        }else {
          reject( response.data.msg );}
      })
      .catch((error) => {
        reject( error );
      });
    })
  },
  post2: function(url,params){
    return new Promise((resolve,reject) => {
      axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
      axios.post(url,QS.stringify(params))
          .then((response) => {
            if(response.data.code ===200){
              resolve( response.data.data );
            }else {
              reject( response.data.msg );}
          })
          .catch((error) => {
            reject( error );
          });
    })
  }
}
export default http